package com.logicwin.aibala.model.androidSWInfo;

import java.sql.Timestamp;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.logicwin.aibala.com.AibalaException;

import android.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Android软件列表项
 * 
 * @author GuoDing
 * @since 2011-05-27
 */
public class APKListItem {
	/**
	 * 软件标识ID
	 */
	private int swID;
	
	/** 
	 * 是否信息可见 
	 */
	private boolean available = false;
	
	/**
	 * 全部信息句柄
	 */
	private APKTotalInfo totalInfo;
	
	/**
	 * XML文件节点信息句柄
	 */
	private Node xmlInfo;
	
	/**
	 * 制定软件标识ID
	 * @param id
	 */
	public APKListItem(Node node) {
		this.xmlInfo = node;
		
		/**
		 * 从XML文件读取基本信息
		 */
		this.swID = Integer.valueOf(this.xmlInfo.getAttributes().getNamedItem("id").getNodeValue());
		NodeList child = this.xmlInfo.getChildNodes();
		for(int i = 0; i < child.getLength(); i++) {
			if(0 == child.item(i).getNodeName().compareTo("apkName")) {
				this.apkName = new String(child.item(i).getNodeValue());
			}
			else if(0 == child.item(i).getNodeName().compareTo("apkSize")) {
				this.apkSize = new String(child.item(i).getNodeValue());
			}
			else if(0 == child.item(i).getNodeName().compareTo("refStar")) {
				this.refStarCount = Integer.valueOf(child.item(i).getNodeValue());
			}
			else if(0 == child.item(i).getNodeName().compareTo("download")) {
				this.downloadCount = new String(child.item(i).getNodeValue());
			}
			else if(0 == child.item(i).getNodeName().compareTo("iconPic")) {
				this.iconPicUrl = new String(child.item(i).getNodeValue());
			}
			else if(0 == child.item(i).getNodeName().compareTo("classID")) {
				this.classID = Integer.valueOf(child.item(i).getNodeValue());
			}
		}
		
		/**
		 * 获取软件安装状态
		 */
		AndroidSWInfoService.checkInstallState(this);
		
		/**
		 * 初始化软件全局句柄
		 */
		this.totalInfo = new APKTotalInfo(this);
	}
	
	/** 
	 * 软件名称 
	 */
	private String apkName;
	
	public String getAPKName() {
		return this.apkName;
	}
	
	/**
	 * 软件版本字符
	 */
	private String versionCode;
	
	public String getVersion() {
		return this.versionCode;
	}
	
	/** 
	 * 图标 
	 */
	private String iconPicUrl;
	private Bitmap iconPic = null;
	private int iconResid = 0;
	
	public Bitmap getIcon() {
		return this.iconPic;
	}
	
	public int getIconResID() {
		return this.iconResid;
	}
	
	/** 
	 * 推荐星数量 
	 */
	private int refStarCount;
	
	public int getRefStarCount() {
		return this.refStarCount;
	}
	
	/** 
	 * 下载次数 
	 */
	private String downloadCount;
	
	public String getDownloadCount() {
		return this.downloadCount;
	}
	
	/** 
	 * 文件大小
	 */
	private String apkSize;
	
	public String getAPKSize() {
		return this.apkSize;
	}
	
	/**
	 * 时间戳，用于排序
	 */
	private Timestamp ts = new Timestamp(System.currentTimeMillis());
	
	public Timestamp getTime() {
		return this.ts;
	}
	
	/**
	 * 类别ID
	 */
	private int classID;
	
	public int getClassID() {
		return this.classID;
	}
	
	/**
	 * 当前安装状态
	 */
	private int currState = APKFileMgmt.APK_STATE_NO_LOAD;
	
	public int getCurrState() {
		return this.currState;
	}
	
	public void setState(int stat) {
		this.currState = stat;
	}
	
	public void reload() {
		/**
		 * 下载icon图标
		 */
		
		this.available = true;
	}
	
	public int getID() {
		return this.swID;
	}
	
	/**
	 * 是否信息已获取
	 * @return
	 */
	public boolean isAvailable() {
		return this.available;
	}
	
	/**
	 * 点击下载
	 * @throws AibalaException
	 */
	public void startDownload() throws AibalaException {
	}
	
	/**
	 * 点击更新
	 * @throws AibalaException
	 */
	public void startUpdate() throws AibalaException {
	}
	
	/**
	 * 停止下载
	 */
	public void stopDownload() {
	}
	
	/**
	 * 获取当前下载的百分比
	 * @return
	 */
	public int getCurrDownload() {
		return 0;
	}
	
	public APKListItem() {
		this.apkName = null;
		this.apkSize = null;
		this.available = false;
		this.classID = 0;
		this.currState = 0;
		this.downloadCount = null;
		this.iconPic = null;
		this.iconPicUrl = null;
		this.refStarCount = 0;
		this.swID = 0;
		this.totalInfo = null;
		this.ts = null;
		this.versionCode = null;
		this.xmlInfo = null;
	}
	
	public static APKListItem getInstance(int index) {
		APKListItem tmp = new APKListItem();
		
		tmp.swID = index;
		tmp.versionCode = "1.0";
		
		switch(index) {
			case 1:
				tmp.apkName = "星际闪电战 STAR BLITZ";
				tmp.refStarCount = 5;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "953";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_1;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic1;
				break;
			case 2:
				tmp.apkName = "贪婪的蜘蛛汉化版";
				tmp.refStarCount = 5;
				tmp.apkSize = "14MB";
				tmp.downloadCount = "1862";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_2;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic2;
				break;
			case 3:
				tmp.apkName = "捕鱼之海底捞";
				tmp.refStarCount = 5;
				tmp.apkSize = "15MB";
				tmp.downloadCount = "15012";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_3;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic3;
				break;
			case 4:
				tmp.apkName = "永恒勇士 ETERNITY WARRIORS";
				tmp.refStarCount = 4;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "4866";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_4;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic4;
				break;
			case 5:
				tmp.apkName = "切水果（汉化版）";
				tmp.refStarCount = 4;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "4611";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_5;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic5;
				break;
			case 6:
				tmp.apkName = "拉手团购";
				tmp.refStarCount = 5;
				tmp.apkSize = "1MB";
				tmp.downloadCount = "18944";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_6;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic6;
				break;
			case 7:
				tmp.apkName = "竞技摩托车";
				tmp.refStarCount = 3;
				tmp.apkSize = "4MB";
				tmp.downloadCount = "1518";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_7;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic7;
				break;
			case 8:
				tmp.apkName = "三国杀手机版";
				tmp.refStarCount = 5;
				tmp.apkSize = "6MB";
				tmp.downloadCount = "22629";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_8;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic8;
				break;
			case 9:
				tmp.apkName = "360手机卫士";
				tmp.refStarCount = 4;
				tmp.apkSize = "15KB";
				tmp.downloadCount = "14661";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_9;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic9;
				break;
			case 10:
				tmp.apkName = "3D极限摩托Trial Xtreme";
				tmp.refStarCount = 5;
				tmp.apkSize = "13MB";
				tmp.downloadCount = "3878";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_10;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic10;
				break;
			case 11:
				tmp.apkName = "烧绳子 Burn the rope";
				tmp.refStarCount = 4;
				tmp.apkSize = "15MB";
				tmp.downloadCount = "2739";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_11;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic11;
				break;
			case 12:
				tmp.apkName = "手机QQ";
				tmp.refStarCount = 4;
				tmp.apkSize = "3MB";
				tmp.downloadCount = "17669";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_12;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic12;
				break;
			case 13:
				tmp.apkName = "武士II复仇 Samurai II: Vengeance";
				tmp.refStarCount = 4;
				tmp.apkSize = "41MB";
				tmp.downloadCount = "5441";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_13;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic13;
				break;
			case 14:
				tmp.apkName = "航海时代2 Age Of Wind 2";
				tmp.refStarCount = 5;
				tmp.apkSize = "23MB";
				tmp.downloadCount = "1,703";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_14;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic14;
				break;
			case 15:
				tmp.apkName = "美女看不完";
				tmp.refStarCount = 5;
				tmp.apkSize = "187KB";
				tmp.downloadCount = "4840";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_15;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic15;
				break;
			case 16:
				tmp.apkName = "掌上书苑";
				tmp.refStarCount = 4;
				tmp.apkSize = "869KB";
				tmp.downloadCount = "2618";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_16;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic16;
				break;
			case 17:
				tmp.apkName = "酷我听听";
				tmp.refStarCount = 3;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "2527";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_17;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic17;
				break;
			case 18:
				tmp.apkName = "酷我电台";
				tmp.refStarCount = 4;
				tmp.apkSize = "982KB";
				tmp.downloadCount = "2142";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_18;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic18;
				break;
			case 19:
				tmp.apkName = "爱看";
				tmp.refStarCount = 3;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "804";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_19;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic19;
				break;
			case 20:
				tmp.apkName = "迷人浏览器";
				tmp.refStarCount = 5;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "877";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_20;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic20;
				break;
			case 21:
				tmp.apkName = "好景";
				tmp.refStarCount = 4;
				tmp.apkSize = "116KB";
				tmp.downloadCount = "4372";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_21;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic21;
				break;
			case 22:
				tmp.apkName = "微信";
				tmp.refStarCount = 5;
				tmp.apkSize = "16KB";
				tmp.downloadCount = "1211";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_22;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic22;
				break;
			case 23:
				tmp.apkName = "海豚浏览器HD";
				tmp.refStarCount = 4;
				tmp.apkSize = "2MB";
				tmp.downloadCount = "2976";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_23;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic23;
				break;
			case 24:
				tmp.apkName = "CC来电炫图";
				tmp.refStarCount = 4;
				tmp.apkSize = "869KB";
				tmp.downloadCount = "2470";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_24;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic24;
				break;
			case 25:
				tmp.apkName = "惠生活";
				tmp.refStarCount = 3;
				tmp.apkSize = "336KB";
				tmp.downloadCount = "951";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_25;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic25;
				break;
			case 26:
				tmp.apkName = "丁丁生活";
				tmp.refStarCount = 3;
				tmp.apkSize = "918KB";
				tmp.downloadCount = "1271";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_26;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic26;
				break;
			case 27:
				tmp.apkName = "掌上亚马逊";
				tmp.refStarCount = 4;
				tmp.apkSize = "1MB";
				tmp.downloadCount = "2205";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_27;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic27;
				break;
			case 28:
				tmp.apkName = "QQ美食";
				tmp.refStarCount = 3;
				tmp.apkSize = "764KB";
				tmp.downloadCount = "1185";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_28;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic28;
				break;
			case 29:
				tmp.apkName = "交通违章-出行无忧";
				tmp.refStarCount = 3;
				tmp.apkSize = "1MB";
				tmp.downloadCount = "1698";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_29;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic29;
				break;
			case 30:
				tmp.apkName = "思根名片神 Think root card god";
				tmp.refStarCount = 5;
				tmp.apkSize = "5MB";
				tmp.downloadCount = "3356";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon_30;
				tmp.detailResID = com.logicwin.aibala.R.drawable.pic30;
				break;
			default:
				tmp.apkName = "Android程序" + String.valueOf(index);
				tmp.refStarCount = index % 4 + 1;
				tmp.downloadCount = "10000";
				tmp.apkSize = "2.5M";
				tmp.iconResid = com.logicwin.aibala.R.drawable.icon;
				tmp.detailResID = com.logicwin.aibala.R.drawable.icon;
				break;
		}
		
//		tmp.apkName = "Android程序" + String.valueOf(index);
//		tmp.versionCode = "1.0";
//		tmp.refStarCount = index % 4 + 1;
//		tmp.downloadCount = "10000";
//		tmp.apkSize = "2.5M";
//		tmp.classID = index % 6 + 1;
		
		return tmp;
	}
	
	private int detailResID;
	
	public Bitmap getDetailPic() {
		return null;
	}
	
	public int getDetailResID() {
		return this.detailResID;
	}
}








